/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProjetoCompreJa.DAO;

import br.com.classesbasicas.Cliente;
import br.com.classesbasicas.Pessoa;
import ProjetoCompreJa.Connect_BD;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author DANIEL
 */
public class ClienteDAO extends Connect_BD {

    EntityManagerFactory emf;
    EntityManager em;

    public ClienteDAO() {
        emf = Persistence.createEntityManagerFactory("compreja");
        em = emf.createEntityManager();
    }

    public void CadastrarCliente(Cliente cli) throws Exception {
        try {
            em.getTransaction().begin();
            em.merge(cli);
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public void ExcluirCliente(Cliente cli) throws Exception {
        try {
            em.getTransaction().begin();
            /*em.remove(produto);*/
            em.remove(em.getReference(Cliente.class, cli.getId()));
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());

        }
    }

    public void AlterarCliente(Cliente cli) throws Exception {
        try {
            em.getTransaction().begin();
            em.merge(cli);
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }

    }

    public List<Cliente> ListarClientes() throws Exception {

        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select cliente from Cliente cliente");
            List<Cliente> cliente = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return cliente;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public List<Cliente> ListarClientesPorNome(Cliente cli) throws Exception {

        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select cliente from Cliente cliente where cliente.nome = '" + cli.getNome() + "'");
            List<Cliente> cliente = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return cliente;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public List<Cliente> ListarClientesPorEstado(Cliente cli) throws Exception {

        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select cliente from Cliente cliente where cliente.endereco.estado = '" + cli.getEndereco().getEstado() + "'");
            List<Cliente> cliente = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return cliente;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public List<Cliente> ListarClientesPorSexo(Cliente cli) throws Exception {

        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select cliente from Cliente cliente where cliente.sexo = '" + cli.getSexo() + "'");
            List<Cliente> cliente = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return cliente;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

}
